﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Presentation.Module.Security.Model;

namespace Presentation.Module.Security.Service.Mapper
{
    internal class UserManagementMapper
    {
        public UserManagementMapper()
        {
            
        }

        internal static Domain.UserManagement.Entities.User ModelToDomain(User user)
        {
            if(user == null)
            {
                throw new ArgumentNullException("user");
            }

            var domainUser = new Domain.UserManagement.Entities.User();
            domainUser.UserName = user.UserName;
            domainUser.UserRole = ModelToDomain(user.UserRole);
            domainUser.UserPassword.HashedPassword = user.UserPassword.HashedPassword;
            domainUser.FirstName = user.FirstName;
            domainUser.LastName = user.LastName;
            domainUser.CompanyId = user.CompanyId;

            return domainUser;
        }

        internal static Domain.UserManagement.Entities.Role ModelToDomain(Role role)
        {
            if(role == null)
            {
                throw new ArgumentNullException("role");
            }

            var domainRole = new Domain.UserManagement.Entities.Role();
            domainRole.Name = role.Name;

            if (role.Permissions.UserManagementPermission == null || role.Permissions.SetupPermission == null || role.Permissions.StudyDefinitionPermission == null || role.Permissions.SchedulingPermission == null || role.Permissions.ReportingPermission == null)
            {
                throw new ArgumentException("role.Permissions");
            }

            domainRole.Permissions.UserManagementPermission.CanRead = role.Permissions.UserManagementPermission.CanRead;
            domainRole.Permissions.UserManagementPermission.CanWrite = role.Permissions.UserManagementPermission.CanWrite;
            domainRole.Permissions.SetupPermission.CanRead = role.Permissions.SetupPermission.CanRead;
            domainRole.Permissions.SetupPermission.CanWrite = role.Permissions.SetupPermission.CanWrite;
            domainRole.Permissions.StudyDefinitionPermission.CanRead = role.Permissions.StudyDefinitionPermission.CanRead;
            domainRole.Permissions.StudyDefinitionPermission.CanWrite = role.Permissions.StudyDefinitionPermission.CanWrite;
            domainRole.Permissions.SchedulingPermission.CanRead = role.Permissions.SchedulingPermission.CanRead;
            domainRole.Permissions.SchedulingPermission.CanWrite = role.Permissions.SchedulingPermission.CanWrite;
            domainRole.Permissions.ReportingPermission.CanRead = role.Permissions.ReportingPermission.CanRead;
            domainRole.Permissions.ReportingPermission.CanWrite = role.Permissions.ReportingPermission.CanWrite;

            return domainRole;
        }

        internal static User DomainToModel(Domain.UserManagement.Entities.User user)
        {
            if(user == null)
            {
                throw new ArgumentNullException("user");
            }

            var modelUser = new User();
            modelUser.UserName = user.UserName;
            modelUser.UserRole = DomainToModel(user.UserRole);
            modelUser.UserPassword.HashedPassword = user.UserPassword.HashedPassword;
            modelUser.FirstName = user.FirstName;
            modelUser.LastName = user.LastName;
            modelUser.CompanyId = user.CompanyId;

            return modelUser;
        }

        internal static Role DomainToModel(Domain.UserManagement.Entities.Role role)
        {
            if (role == null)
            {
                throw new ArgumentNullException("role");
            }

            var modelRole = new Role();
            modelRole.Name = role.Name;

            if (role.Permissions.UserManagementPermission == null || role.Permissions.SetupPermission == null || role.Permissions.StudyDefinitionPermission == null || role.Permissions.SchedulingPermission == null || role.Permissions.ReportingPermission == null)
            {
                throw new ArgumentException("role.Permissions");
            }

            modelRole.Permissions.UserManagementPermission.CanRead = role.Permissions.UserManagementPermission.CanRead;
            modelRole.Permissions.UserManagementPermission.CanWrite = role.Permissions.UserManagementPermission.CanWrite;
            modelRole.Permissions.SetupPermission.CanRead = role.Permissions.SetupPermission.CanRead;
            modelRole.Permissions.SetupPermission.CanWrite = role.Permissions.SetupPermission.CanWrite;
            modelRole.Permissions.StudyDefinitionPermission.CanRead = role.Permissions.StudyDefinitionPermission.CanRead;
            modelRole.Permissions.StudyDefinitionPermission.CanWrite = role.Permissions.StudyDefinitionPermission.CanWrite;
            modelRole.Permissions.SchedulingPermission.CanRead = role.Permissions.SchedulingPermission.CanRead;
            modelRole.Permissions.SchedulingPermission.CanWrite = role.Permissions.SchedulingPermission.CanWrite;
            modelRole.Permissions.ReportingPermission.CanRead = role.Permissions.ReportingPermission.CanRead;
            modelRole.Permissions.ReportingPermission.CanWrite = role.Permissions.ReportingPermission.CanWrite;

            return modelRole;
        }

#region  ModelToDomain - Permissions


        internal static Domain.UserManagement.Entities.PermissionUserManagement ModelToDomain(PermissionUserManagement userManagementPermission)
        {
            if(userManagementPermission == null)
            {
                throw new ArgumentNullException("userManagementPermission");
            }

            var domainPermission = new Domain.UserManagement.Entities.PermissionUserManagement();

            domainPermission.CanRead = userManagementPermission.CanRead;
            domainPermission.CanWrite = userManagementPermission.CanWrite;

            return domainPermission;
        }

        internal static Domain.UserManagement.Entities.PermissionSetup ModelToDomain(PermissionSetup setupPermission)
        {
            if (setupPermission == null)
            {
                throw new ArgumentNullException("setupPermission");
            }

            var domainPermission = new Domain.UserManagement.Entities.PermissionSetup();

            domainPermission.CanRead = setupPermission.CanRead;
            domainPermission.CanWrite = setupPermission.CanWrite;

            return domainPermission;
        }

        internal static Domain.UserManagement.Entities.PermissionStudyDefinition ModelToDomain(PermissionStudyDefinition studyDefinitionPermission)
        {
            if (studyDefinitionPermission == null)
            {
                throw new ArgumentNullException("studyDefinitionPermission");
            }

            var domainPermission = new Domain.UserManagement.Entities.PermissionStudyDefinition();

            domainPermission.CanRead = studyDefinitionPermission.CanRead;
            domainPermission.CanWrite = studyDefinitionPermission.CanWrite;

            return domainPermission;
        }

        internal static Domain.UserManagement.Entities.PermissionScheduling ModelToDomain(PermissionScheduling schedulingPermission)
        {
            if (schedulingPermission == null)
            {
                throw new ArgumentNullException("schedulingPermission");
            }

            var domainPermission = new Domain.UserManagement.Entities.PermissionScheduling();

            domainPermission.CanRead = schedulingPermission.CanRead;
            domainPermission.CanWrite = schedulingPermission.CanWrite;

            return domainPermission;
        }

        internal static Domain.UserManagement.Entities.PermissionReporting ModelToDomain(PermissionReporting reportingPermission)
        {
            if (reportingPermission == null)
            {
                throw new ArgumentNullException("reportingPermission");
            }

            var domainPermission = new Domain.UserManagement.Entities.PermissionReporting();

            domainPermission.CanRead = reportingPermission.CanRead;
            domainPermission.CanWrite = reportingPermission.CanWrite;

            return domainPermission;
        }
        #endregion

#region ModelToDomain - Permission

        internal static PermissionUserManagement DomainToModel(Domain.UserManagement.Entities.PermissionUserManagement userManagementPermission)
        {
            if (userManagementPermission == null)
            {
                throw new ArgumentNullException("userManagementPermission");
            }

            var domainPermission = new PermissionUserManagement();

            domainPermission.CanRead = userManagementPermission.CanRead;
            domainPermission.CanWrite = userManagementPermission.CanWrite;

            return domainPermission;
        }

        internal static PermissionSetup DomainToModel(Domain.UserManagement.Entities.PermissionSetup setupPermission)
        {
            if (setupPermission == null)
            {
                throw new ArgumentNullException("setupPermission");
            }

            var domainPermission = new PermissionSetup();

            domainPermission.CanRead = setupPermission.CanRead;
            domainPermission.CanWrite = setupPermission.CanWrite;

            return domainPermission;
        }

        internal static PermissionStudyDefinition DomainToModel(Domain.UserManagement.Entities.PermissionStudyDefinition studyDefinitionPermission)
        {
            if (studyDefinitionPermission == null)
            {
                throw new ArgumentNullException("studyDefinitionPermission");
            }

            var domainPermission = new PermissionStudyDefinition();

            domainPermission.CanRead = studyDefinitionPermission.CanRead;
            domainPermission.CanWrite = studyDefinitionPermission.CanWrite;

            return domainPermission;
        }

        internal static PermissionScheduling DomainToModel(Domain.UserManagement.Entities.PermissionScheduling schedulingPermission)
        {
            if (schedulingPermission == null)
            {
                throw new ArgumentNullException("schedulingPermission");
            }

            var domainPermission = new PermissionScheduling();

            domainPermission.CanRead = schedulingPermission.CanRead;
            domainPermission.CanWrite = schedulingPermission.CanWrite;

            return domainPermission;
        }

        internal static PermissionReporting DomainToModel(Domain.UserManagement.Entities.PermissionReporting reportingPermission)
        {
            if (reportingPermission == null)
            {
                throw new ArgumentNullException("reportingPermission");
            }

            var domainPermission = new PermissionReporting();

            domainPermission.CanRead = reportingPermission.CanRead;
            domainPermission.CanWrite = reportingPermission.CanWrite;

            return domainPermission;
        }
#endregion
    }
}
